# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
import os
import sys
import getpass
import shutil

class Ui_Form(object):
    def save(self):
        username = str(self.lineEdit_username.text())
        eth = str(self.lineEdit_eth.text())
        dns1 = str(self.lineEdit_dns1.text())
        dns2 = str(self.lineEdit_dns2.text())
        password = str(self.lineEdit_password.text())
        firewall = str(self.lineEdit_firewall.text())
        shutil.copyfile("/etc/ppp/pppoe.conf", "/etc/ppp/pppoe.conf-backup")
        file=open("/etc/ppp/pppoe.conf","w")
        file.write("ETH='" + eth + "'")
        file.write("\nUSER='" + username + "'")
        file.write("\nDEMAND=no")
        file.write("\nDNSTYPE=SPECIFY")
        file.write("\nPEERDNS=no")
        file.write("\nDNS1=" + dns1)
        file.write("\nDNS2=" + dns2)
        file.write("\nDEFAULTROUTE=yes")
        file.write("\nCONNECT_TIMEOUT=30")
        file.write("\nCONNECT_POLL=2")
        file.write("\nACNAME=")
        file.write("\nSERVICENAME=")
        file.write("\nPING=" + "\"" + "." + "\"")
        file.write("\nCF_BASE=`basename $CONFIG`")
        file.write("\nPIDFILE=" +"\"" + "/var/run/$CF_BASE-pppoe.pid" +"\"")
        file.write("\nSYNCHRONOUS=no")
        file.write("\nCLAMPMSS=1412")
        file.write("\nLCP_INTERVAL=20")
        file.write("\nLCP_FAILURE=3")
        file.write("\nPPPOE_TIMEOUT=80")
        if (firewall==0):
            file.write("\nFIREWALL=NONE")
        if(firewall==1):
            file.write("\nFIREWALL=STANDALONE")
        if(firewall==2):
            file.write("\nFIREWALL=MASQUERADE")
        file.write("\nLINUX_PLUGIN=")
        file.write("\nPPPOE_EXTRA=" + "\"" + "\"")
        file.write("\nPPPD_EXTRA=" + "\"" + "\"")
        file.close()
        shutil.copyfile("/etc/ppp/pap-secrets", "/etc/ppp/pap-secrets-backup")
        file=open("/etc/ppp/pap-secrets","w")
        file.write("# Secrets for authentication using PAP")
        file.write("\n# client server secret IP addresses\n")
        file.write("\"" + username + "\"" + " * " + "\"" + password + "\"")
        file.close()
        shutil.copyfile("/etc/ppp/chap-secrets", "/etc/ppp/chap-secrets-backup")
        file=open("/etc/ppp/chap-secrets","w")
        file.write("# Secrets for authentication using CHAP")
        file.write("\n# client server secret IP addresses\n")
        file.write("\"" + username + "\"" + " * " + "\"" + password + "\"")
        file.close()
        self.label_username.setEnabled(False)
        self.lineEdit_username.setEnabled(False)
        self.label_eth.setEnabled(False)
        self.lineEdit_eth.setEnabled(False)
        self.label_dns1.setEnabled(False)
        self.lineEdit_dns1.setEnabled(False)
        self.label_dns2.setEnabled(False)
        self.lineEdit_dns2.setEnabled(False)
        self.label_password.setEnabled(False)
        self.lineEdit_password.setEnabled(False)
        self.label_firewall.setEnabled(False)
        self.lineEdit_firewall.setEnabled(False)
        self.settings_button.setEnabled(True)
        self.save_button.setEnabled(False)
        self.start_button.setEnabled(True)
        file=open("settings","w")
        file.write(username + "\n")
        file.write(eth + "\n")
        file.write(dns1 + "\n")
        file.write(dns2 + "\n")
        file.write(password + "\n")
        file.write(firewall + "\n")
        file.close()
        os.chmod("settings",0444)
    def start(self):
        os.system("pppoe-start")
    def settings(self):
        self.label_username.setEnabled(True)
        self.lineEdit_username.setEnabled(True)
        self.label_eth.setEnabled(True)
        self.lineEdit_eth.setEnabled(True)
        self.label_dns1.setEnabled(True)
        self.lineEdit_dns1.setEnabled(True)
        self.label_dns2.setEnabled(True)
        self.lineEdit_dns2.setEnabled(True)
        self.label_password.setEnabled(True)
        self.lineEdit_password.setEnabled(True)
        self.label_firewall.setEnabled(True)
        self.lineEdit_firewall.setEnabled(True)
        self.settings_button.setEnabled(False)
        self.save_button.setEnabled(True)
        self.start_button.setEnabled(False)
    def setupUi(self, Form):
        if os.getenv('USER') != 'root':
            sys.exit("Please run the application as root")
        Form.setObjectName("Form")
        Form.setEnabled(True)
        Form.resize(430, 470)
        Form.setMinimumSize(QtCore.QSize(430, 470))
        Form.setMaximumSize(QtCore.QSize(430, 470))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("pppoegui.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        Form.setWindowIcon(icon)
        self.label_username = QtGui.QLabel(Form)
        self.label_username.setEnabled(False)
        self.label_username.setGeometry(QtCore.QRect(10, 10, 184, 19))
        self.label_username.setObjectName("label_username")
        self.lineEdit_username = QtGui.QLineEdit(Form)
        self.lineEdit_username.setEnabled(False)
        self.lineEdit_username.setGeometry(QtCore.QRect(10, 30, 411, 27))
        self.lineEdit_username.setObjectName("lineEdit_username")
        self.label_eth = QtGui.QLabel(Form)
        self.label_eth.setEnabled(False)
        self.label_eth.setGeometry(QtCore.QRect(10, 80, 376, 19))
        self.label_eth.setObjectName("label_eth")
        self.lineEdit_eth = QtGui.QLineEdit(Form)
        self.lineEdit_eth.setEnabled(False)
        self.lineEdit_eth.setGeometry(QtCore.QRect(10, 100, 411, 27))
        self.lineEdit_eth.setReadOnly(False)
        self.lineEdit_eth.setObjectName("lineEdit_eth")
        self.label_dns1 = QtGui.QLabel(Form)
        self.label_dns1.setEnabled(False)
        self.label_dns1.setGeometry(QtCore.QRect(10, 150, 253, 19))
        self.label_dns1.setObjectName("label_dns1")
        self.lineEdit_dns1 = QtGui.QLineEdit(Form)
        self.lineEdit_dns1.setEnabled(False)
        self.lineEdit_dns1.setGeometry(QtCore.QRect(10, 170, 411, 27))
        self.lineEdit_dns1.setObjectName("lineEdit_dns1")
        self.label_dns2 = QtGui.QLabel(Form)
        self.label_dns2.setEnabled(False)
        self.label_dns2.setGeometry(QtCore.QRect(10, 220, 295, 19))
        self.label_dns2.setObjectName("label_dns2")
        self.lineEdit_dns2 = QtGui.QLineEdit(Form)
        self.lineEdit_dns2.setEnabled(False)
        self.lineEdit_dns2.setGeometry(QtCore.QRect(10, 240, 411, 27))
        self.lineEdit_dns2.setObjectName("lineEdit_dns2")
        self.label_password = QtGui.QLabel(Form)
        self.label_password.setEnabled(False)
        self.label_password.setGeometry(QtCore.QRect(10, 290, 224, 19))
        self.label_password.setObjectName("label_password")
        self.lineEdit_password = QtGui.QLineEdit(Form)
        self.lineEdit_password.setEnabled(False)
        self.lineEdit_password.setGeometry(QtCore.QRect(10, 310, 411, 27))
        self.lineEdit_password.setAutoFillBackground(False)
        self.lineEdit_password.setEchoMode(QtGui.QLineEdit.Password)
        self.lineEdit_password.setObjectName("lineEdit_password")
        self.label_firewall = QtGui.QLabel(Form)
        self.label_firewall.setEnabled(False)
        self.label_firewall.setGeometry(QtCore.QRect(10, 360, 197, 19))
        self.label_firewall.setObjectName("label_firewall")
        self.lineEdit_firewall = QtGui.QLineEdit(Form)
        self.lineEdit_firewall.setEnabled(False)
        self.lineEdit_firewall.setGeometry(QtCore.QRect(10, 380, 411, 27))
        self.lineEdit_firewall.setReadOnly(False)
        self.lineEdit_firewall.setObjectName("lineEdit_firewall")
        self.save_button = QtGui.QPushButton(Form)
        self.save_button.setEnabled(False)
        self.save_button.setGeometry(QtCore.QRect(150, 420, 131, 29))
        self.save_button.setCursor(QtCore.Qt.PointingHandCursor)
        self.save_button.setObjectName("save_button")
        self.start_button = QtGui.QPushButton(Form)
        self.start_button.setGeometry(QtCore.QRect(290, 420, 131, 29))
        self.start_button.setCursor(QtCore.Qt.PointingHandCursor)
        self.start_button.setObjectName("start_button")
        self.settings_button = QtGui.QPushButton(Form)
        self.settings_button.setGeometry(QtCore.QRect(10, 420, 131, 29))
        self.settings_button.setCursor(QtCore.Qt.PointingHandCursor)
        self.settings_button.setObjectName("settings_button")
        file=open("settings","r")
        line1=file.readline()
        self.lineEdit_username.setText(line1.strip())
        line2=file.readline()
        self.lineEdit_eth.setText(line2.strip())
        line3=file.readline()
        self.lineEdit_dns1.setText(line3.strip())
        line4=file.readline()
        self.lineEdit_dns2.setText(line4.strip())
        line5=file.readline()
        self.lineEdit_password.setText(line5.strip())
        line6=file.readline()
        self.lineEdit_firewall.setText(line6.strip())
        file.close()

        self.retranslateUi(Form)
        QtCore.QObject.connect(self.start_button, QtCore.SIGNAL("clicked()"), self.start)
        QtCore.QObject.connect(self.save_button, QtCore.SIGNAL("clicked()"), self.save)
        QtCore.QObject.connect(self.settings_button, QtCore.SIGNAL("clicked()"), self.settings)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "PPPoE Connection Tool", None, QtGui.QApplication.UnicodeUTF8))
        self.label_username.setText(QtGui.QApplication.translate("Form", "Enter your PPPoE user name", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_username.setToolTip(QtGui.QApplication.translate("Form", "PPPoE user name", None, QtGui.QApplication.UnicodeUTF8))
        self.label_eth.setText(QtGui.QApplication.translate("Form", "Enter the Ethernet interface connected to the DSL modem", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_eth.setToolTip(QtGui.QApplication.translate("Form", "Ethernet interface connected to DSL modem\n"
"For Linux, it will be ethn, where \'n\' is a number", None, QtGui.QApplication.UnicodeUTF8))
        self.label_dns1.setText(QtGui.QApplication.translate("Form", "Enter the first DNS server address here", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_dns1.setToolTip(QtGui.QApplication.translate("Form", "First DNS server address\n"
"Usage: x.x.x.x ", None, QtGui.QApplication.UnicodeUTF8))
        self.label_dns2.setText(QtGui.QApplication.translate("Form", "Enter the secondary DNS server address here", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_dns2.setToolTip(QtGui.QApplication.translate("Form", "Second DNS server address\n"
"Usage: x.x.x.x ", None, QtGui.QApplication.UnicodeUTF8))
        self.label_password.setText(QtGui.QApplication.translate("Form", "Please enter your PPPoE password", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_password.setToolTip(QtGui.QApplication.translate("Form", "PPPoE password", None, QtGui.QApplication.UnicodeUTF8))
        self.label_firewall.setText(QtGui.QApplication.translate("Form", "Choose a type of firewall (0-2)", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_firewall.setToolTip(QtGui.QApplication.translate("Form", "Please enter a number from 0 to 2", None, QtGui.QApplication.UnicodeUTF8))
        self.save_button.setText(QtGui.QApplication.translate("Form", "Save", None, QtGui.QApplication.UnicodeUTF8))
        self.start_button.setText(QtGui.QApplication.translate("Form", "Start PPPoE", None, QtGui.QApplication.UnicodeUTF8))
        self.settings_button.setText(QtGui.QApplication.translate("Form", "Settings", None, QtGui.QApplication.UnicodeUTF8))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

